package com.atguigu.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DictListener extends AnalysisEventListener<DictEeVo> {

    //注入mapper以便操作数据库
    @Autowired
    private DictMapper dictMapper;

    //或者不交给spring管理 在DictServiceImpl层的方法里传入 new DictListener(dictMapper)
//    public DictListener(DictMapper dictMapper) {
//        this.dictMapper = dictMapper;
//    }

    //一行一行的读取excel的内容 从第二行开始读 封装到对象
    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        //添加到数据库
        Dict dict = new Dict();
        //vo类转变为实体类
        BeanUtils.copyProperties(dictEeVo, dict);
        dictMapper.insert(dict);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
